﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using System.Reflection;

namespace NusyaTester
{
    public partial class mainForm : Form
    {
        TextBox Question;
        RichTextBox Report;
        List<RadioButton> Answers;
        Label AnswerLabel;
        TextBox UserAnswer;
        TestHolder Quiz;
        ResourceManager txt = new ResourceManager("NusyaTester.strings", Assembly.GetExecutingAssembly());
        public mainForm()
        {
            InitializeComponent();
            qstnList.SmallImageList = imageList1;
        }

        private void loadBtn_Click(object sender, EventArgs e) {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            Quiz = new TestHolder();
            Quiz.Load(ofd.FileName);
            qstnList.Items.Clear();
            for (int i=1;i<=Quiz.Count;i++) {
                StringBuilder sb = new StringBuilder();
                sb.Append(txt.GetString("Question"));
                sb.Append(i.ToString());
                qstnList.Items.Add(sb.ToString());
            }
            nextBtn.Visible = true;
            reportBtn.Visible = false;
            this.Text = "Nusya Tester : " + ofd.SafeFileName;
            showCurrEntry();
        }

      

        private void displayAnswers(TestEntry te) {
            int rbX = 10; int rbY = 120;
            if (te.EntryType == TestType.VARIANTS) {
                Answers = new List<RadioButton>();
                RadioButton rb;
                foreach (string str in te.Variants) {
                    rb = new RadioButton();
                    rb.Text = str;
                    rb.Location = new Point(rbX, rbY);
                    rbY += 24;
                    Answers.Add(rb);
                    basePanel.Controls.Add(rb);
                }
            }
            if (te.EntryType == TestType.MISS_VALUE) {
                AnswerLabel = new Label();
                AnswerLabel.Text = txt.GetString("EnterAnswer");
                AnswerLabel.Location = new Point(rbX, rbY);
                rbY += AnswerLabel.Height + 8;
                UserAnswer = new TextBox();
                UserAnswer.Location = new Point(rbX, rbY);
                basePanel.Controls.Add(AnswerLabel);
                basePanel.Controls.Add(UserAnswer);
            }
        }

        private void displayQuestion(TestEntry te) {
            Question = new TextBox();
            initTextControl(Question);
            Question.AutoSize = true;
            Question.Size = new System.Drawing.Size(250, 100);
            Question.TabIndex = 3;
            Question.Text = te.Question;
            basePanel.Controls.Add(Question);
        }

        private void initTextControl(TextBoxBase ctl) {
            ctl.ReadOnly = true;
            ctl.BackColor = System.Drawing.SystemColors.Control;
            ctl.BorderStyle = System.Windows.Forms.BorderStyle.None;
            ctl.Multiline = true;
            ctl.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            ctl.Location = new Point(10, 12);
            ctl.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
        }

        private void displayShortReport() {
            basePanel.Controls.Clear();
            Report = new RichTextBox();
            initTextControl(Report);
            Report.Size = new System.Drawing.Size(250, 250);
            Report.AppendText(txt.GetString("TotalQnt") + Quiz.Count+Environment.NewLine);
            basePanel.Controls.Add(Report);
            Quiz.calculateResut();
            Report.SelectionColor = Color.Green;
            Report.AppendText(txt.GetString("RightAnsws") + Quiz.RightCount + Environment.NewLine);
            Report.SelectionColor = Color.Red;
            Report.AppendText(txt.GetString("WrongAnsws") + Quiz.WrongCount + Environment.NewLine);
        }


        private bool getAnswer() {
            if (Quiz.currentEntry.EntryType == TestType.VARIANTS) {
                foreach (RadioButton rb in Answers) {
                    if (rb.Checked) {
                        Quiz.currentEntry.givenAnswer = rb.Text;
                        return true;
                    }
                }
            }
            if (Quiz.currentEntry.EntryType == TestType.MISS_VALUE) {
                if (UserAnswer.Text.Length > 0) {
                    Quiz.currentEntry.givenAnswer = UserAnswer.Text;
                    return true;
                }
                }
            return false;
        }

        private void nextBtn_Click(object sender, EventArgs e) {
            if (getAnswer() == false) return;
            if (Quiz.currentEntry.isCorrect) {
                qstnList.Items[Quiz.currIndex].ImageIndex = 1;
            } else {
                qstnList.Items[Quiz.currIndex].ImageIndex = 0;
            }
            if (Quiz.EndOfTest) {
                nextBtn.Visible = false;
                reportBtn.Visible = true;
                displayShortReport();
                return;
            }
            Quiz.Next();
            showCurrEntry();
        }

        private void showCurrEntry() {
            basePanel.Controls.Clear();
            displayQuestion(Quiz.currentEntry);
            displayAnswers(Quiz.currentEntry);
            qstnList.Items[Quiz.currIndex].ImageIndex = 2;
        }

        private void reportBtn_Click(object sender, EventArgs e) {
            displayReport();
            reportBtn.Visible = false;
        }

        private void displayReport() {
            string NL = Environment.NewLine;
            Report.AppendText(NL);
            foreach (TestEntry te in Quiz.BadAnswers) {
                Report.SelectionColor = Color.Sienna;
                Report.AppendText(txt.GetString("GivenAnswer") + te.givenAnswer + NL);
                Report.AppendText(txt.GetString("RightAnswer") + te.correctAnswer + NL);
                Report.AppendText(te.Explanation + NL + NL);
            }
        }
    }
}
